package com.ym.ganio.gan;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.ym.ganio.R;
import com.ym.ganio.article.ArticleActivity;
import com.ym.ganio.define.ReadDefine;
import com.ym.ganio.net.GanApiNetHelper;
import com.ym.ganio.net.GanApiResult;
import com.yumo.common.net.YmNetUtil;
import com.yumo.common.ui.DividerItemDecoration;
import com.yumo.common.ui.RecyclerViewItemClickListener;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yumodev on 17/5/31.
 * 干货列表数据
 */
public class GanFragment extends Fragment {
    private final String LOG_TAG = "GanFragment";

    private String mType = "";
    private RecyclerView mListView;
    private SwipeRefreshLayout mRefreshLayout;
    private GanAdapter mAdapter;
    private ContentLoadingProgressBar mLoadingView;
    private int mPageCount = 10;
    private int mPageNum = 1;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(LOG_TAG, "onCreate");
        Bundle bundle = getArguments();
        if (bundle != null){
            mType = bundle.getString(ReadDefine.PARAM_KEY_TYPE);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.gan_list_page, null, false);
        setupUI(view);
        loadData(mPageCount, mPageNum);
        return view;
    }

    private void setupUI(View view){
        mListView = (RecyclerView)view.findViewById(R.id.list_view);
        mListView.setLayoutManager(new LinearLayoutManager(getContext()));
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST, getResources().getDrawable(R.color.common_divider));
        dividerItemDecoration.setHeight(getResources().getDimensionPixelSize(R.dimen.common_divider_height));
        mListView.addItemDecoration(dividerItemDecoration);
        mAdapter = new GanAdapter(getContext());
        mAdapter.setFooterView(View.inflate(getContext(), R.layout.gan_list_loadmore, null));
        mAdapter.setItemClickListener(new RecyclerViewItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                Intent intent = new Intent(getActivity(), ArticleActivity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable(ReadDefine.PARAME_KEY_ARTICLE, mAdapter.getItemData(position));
                intent.putExtra("param", bundle);
                startActivity(intent);
            }
        });

        mListView.setAdapter(mAdapter);

        mRefreshLayout = (SwipeRefreshLayout)view.findViewById(R.id.refresh_layout);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadData(mPageCount, 1);
            }
        });

        mListView.addOnScrollListener(new RecyclerViewLoadMoreScrollListener(mListView.getLayoutManager()) {
            @Override
            protected void loadMore() {
                loadData(mPageCount, mPageNum++);
            }
        });

        mLoadingView = (ContentLoadingProgressBar)view.findViewById(R.id.progressbar);
        if (YmNetUtil.hasNet(getContext())){
            showLoadingView();
        }
    }

    private void loadData(final int pageCount, final int pageNum){
        if (!YmNetUtil.hasNet(getContext())){
            Toast.makeText(getContext(), R.string.common_no_net, Toast.LENGTH_SHORT).show();
            mRefreshLayout.setRefreshing(false);
            return;
        }
        Observable.create(new Observable.OnSubscribe<GanApiResult>() {
            @Override
            public void call(Subscriber<? super GanApiResult> subscriber) {
                GanApiResult result = GanApiNetHelper.getTypeDataByPage(mType, pageCount, pageNum);
                subscriber.onNext(result);
            }
        })  .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<GanApiResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingView();
                    }

                    @Override
                    public void onNext(GanApiResult s) {
                        if (s != null && !s.isError()){
                            mAdapter.addDataList(s.getResults());
                            mAdapter.notifyDataSetChanged();
                        }else{
                            Toast.makeText(getContext(), "failed", Toast.LENGTH_SHORT).show();
                        }
                        mRefreshLayout.setRefreshing(false);
                        hideLoadingView();
                    }
                });
    }

    private void showLoadingView(){
        if (mLoadingView != null){
            mLoadingView.setVisibility(View.VISIBLE);
        }
    }

    private void hideLoadingView(){
        if (mLoadingView != null){
            mLoadingView.setVisibility(View.GONE);
        }
    }


}
